package com.design.adnroid.campussecondarytrading.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import com.design.adnroid.campussecondarytrading.AppContext;
import com.design.adnroid.campussecondarytrading.database.DBOperate;
import com.design.adnroid.campussecondarytrading.entity.Person;

public class PersonalEditActivity extends AppCompatActivity {

    private TextView tvUserName;
    private EditText etName;
    private EditText etOldPwd, etNewPwd, etNewPwdSure;
    private Spinner spinner;
    private Button btChangedPwd, btSureChanged;
    private LinearLayout linearLayout;
    private DBOperate dbOperate;
    private Person person;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_edit);

        initID();
        initView();
//
        btChangedPwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int i = linearLayout.getVisibility();
                if (i == View.VISIBLE) {
                    linearLayout.setVisibility(View.GONE);
                } else if (i == View.GONE) {
                    linearLayout.setVisibility(View.VISIBLE);
                }
            }
        });
//
        btSureChanged.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                upDate();
            }
        });
    }//onCreate

    private void upDate() {
        String name = etName.getText().toString();
        String sex = spinner.getSelectedItem().toString();
        String oldUserPwd = etOldPwd.getText().toString();
        String newUserPwd = etNewPwd.getText().toString();
        String newUserPwdSure = etNewPwdSure.getText().toString();

        if (name.isEmpty()) {
            etName.requestFocus();
            etName.setError("昵称为空!");
            return;
        }
        if (linearLayout.getVisibility()==View.VISIBLE) {
            if (oldUserPwd.isEmpty()) {
                etOldPwd.requestFocus();
                etOldPwd.setError("原密码为空!");
                return;
            }
            if (!oldUserPwd.equals(person.getUserPwd())) {
                etOldPwd.requestFocus();
                etOldPwd.setError("原密码错误!");
                return;
            }
            if (newUserPwd.isEmpty()) {
                etNewPwd.requestFocus();
                etNewPwd.setError("新密码为空!");
                return;
            }
            if (newUserPwdSure.isEmpty()) {
                etNewPwdSure.requestFocus();
                etNewPwdSure.setError("匹配密码为空!");
                return;
            }
            if (!newUserPwd.equals(newUserPwdSure)) {
                etNewPwdSure.requestFocus();
                etNewPwdSure.setError("密码不匹配!");
                return;
            }
            person.setUserPwd(newUserPwdSure);
        }
        person.setName(name);
        person.setSex(sex);
        dbOperate.openDB();
        dbOperate.upPerson(person);
        dbOperate.closeDb();

        AppContext.setUserName(person.getUserName());
        AppContext.setName(person.getName());
        AppContext.setUserMd5(person.getUserMd5());

        Intent intent =new Intent(this,MainActivity.class);
        startActivity(intent);
        finish();
    }

    private void initView() {
        dbOperate = new DBOperate(this);
        dbOperate.openDB();
        person = dbOperate.getPerson(AppContext.getUserName());
        dbOperate.closeDb();

        tvUserName.setText(person.getUserName());
        etName.setText(person.getName());

        spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                new String[]{"男", "女"}));
        if (person.getSex().equals("男")) {
            spinner.setSelection(0);
        } else if (person.getSex().equals("女")) {
            spinner.setSelection(1);
        }
        linearLayout.setVisibility(View.GONE);
    }

    private void initID() {
        tvUserName = findViewById(R.id.personal_edit_userName);

        etName = findViewById(R.id.personal_edit_name);
        spinner = findViewById(R.id.personal_edit_sex);

        btChangedPwd = findViewById(R.id.personal_edit_bt);
        linearLayout = findViewById(R.id.personal_edit_layout);

        etOldPwd = findViewById(R.id.personal_edit_oldPwd);
        etNewPwd = findViewById(R.id.personal_edit_newPwd);
        etNewPwdSure = findViewById(R.id.personal_edit_newPwdSure);

        btSureChanged = findViewById(R.id.personal_edit_bt2);

    }


}
